package com.shujia.base.day04

import java.sql.DriverManager

/**
 *  jdbc六步骤：
 *  1、注册驱动
 *  2、创建数据库连接对象
 *  3、创建数据库操作对象
 *  4、执行sql语句
 *  5、当第4步是查询的话，解析查询结果
 *  6、释放资源
 */
object Demo23JDBC {
  def main(args: Array[String]): Unit = {
    //1、注册驱动
    Class.forName("com.mysql.jdbc.Driver")

    //2、创建数据库连接对象
    val conn = DriverManager.getConnection(
      "jdbc:mysql://master:3306/hefei_metro_warehouse?useUnicode=true&characterEncoding=UTF-8&useSSL=false",
      "root",
      "123456"
    )

    //3、创建数据库操作对象
    val preparedStatement = conn.prepareStatement("select * from hefei_metro_swipe_data where cId=?")

    preparedStatement.setString(1,"6587327007")

    //4、执行sql语句
    val resultSet = preparedStatement.executeQuery()

    //5、当第4步是查询的话，解析查询结果
    while (resultSet.next()){
      val line = resultSet.getString("line")
      val cId = resultSet.getString("cId")
      val shuaTime = resultSet.getString("shuaTime")
      val type_name = resultSet.getString("type")
      val balance = resultSet.getString("balance")
      val amount = resultSet.getString("amount")
      val sId = resultSet.getString("sId")
      val station = resultSet.getString("station")
      val day = resultSet.getString("day")
      val flag = resultSet.getString("flag")
      val Recharge = resultSet.getString("Recharge")

      println(s"${line},${cId},${shuaTime},${type_name},${balance},${amount},${sId},${station},${day},${flag},${Recharge}")
    }

    //6、释放资源
    preparedStatement.close()
    conn.close()




  }
}
